package com.lopo.sms;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TxSms {
    private static final String OK = "Ok";
    private static final String FAILED = " failed: ";
    private static final String SEND = "Send verification code to ";

    public static boolean sendRegisterCode(String phoneNo){
        try {
            Credential cred = new Credential("AKIDoOilZ7OO6XhaBVYJn0vZw1qOfeNoyqSn", "4rNcZFdk7D2assb3E8k59wRfcJjriJce");

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);

            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {phoneNo};
            req.setPhoneNumberSet(phoneNumberSet1);

            String[] templateParamSet = {"lopo", "zone"};
            req.setTemplateParamSet(templateParamSet);
            req.setSmsSdkAppId("1400538929");
            req.setSignName("逸动科技");
            req.setTemplateId("1480795");

            SendSmsResponse resp = client.SendSms(req);

            if (!resp.getSendStatusSet()[0].getCode().equals("Ok")) {
                log.error("Send verification code to {} failed: {}" , phoneNo, resp.getSendStatusSet()[0].getMessage());
            }
            return true;
        } catch (Exception e) {
            log.error("Send verification code to {} failed: {}" , phoneNo, e.getMessage());
        }
        return false;
    }

    public static void main(String[] args) throws Exception {
        sendRegisterCode("17512877661");
    }

}
